################################################################################
# BAREFOOT NETWORKS CONFIDENTIAL & PROPRIETARY
#
# Copyright (c) 2018-2019 Barefoot Networks, Inc.

# All Rights Reserved.
#
# NOTICE: All information contained herein is, and remains the property of
# Barefoot Networks, Inc. and its suppliers, if any. The intellectual and
# technical concepts contained herein are proprietary to Barefoot Networks,
# Inc.
# and its suppliers and may be covered by U.S. and Foreign Patents, patents in
# process, and are protected by trade secret or copyright law.
# Dissemination of this information or reproduction of this material is
# strictly forbidden unless prior written permission is obtained from
# Barefoot Networks, Inc.
#
# No warranty, explicit or implicit is provided, unless granted under a
# written agreement with Barefoot Networks, Inc.
#
# Milad Sharif (msharif@barefootnetworks.com)
#
###############################################################################

import logging

import ptf.testutils as testutils
from bfruntime_base_tests import BfRuntimeTest
import bfrt_grpc.bfruntime_pb2 as bfruntime_pb2

logger = logging.getLogger('Test')
logger.addHandler(logging.StreamHandler())

class TernaryMatchTest(BfRuntimeTest):
    def runTest(self):
        ig_port = 1
        eg_ports = [2, 3]
        dip = '10.10.0.1'

        pkt = testutils.simple_tcp_packet(ip_dst=dip)
        exp_pkt = pkt

        target = self.Target(device_id=0, pipe_id=0xffff)
        self.insert_table_entry(
            target,
            'SwitchIngress.forward',
            [self.KeyField('$MATCH_PRIORITY', self.to_bytes(1, 4)),
             self.KeyField('vrf', '\x00\x00'),
             self.KeyField('hdr.ipv4.dst_addr',
                           self.ipv4_to_bytes(dip),
                           self.ipv4_to_bytes('255.255.0.0'))],
            'SwitchIngress.hit',
            [self.DataField('port', '\x00\x02')])

        #TODO(msharif): Uncomment this when TCAM priority is implemented
        #self.insert_table_entry(
        #    target,
        #    'SwitchIngress.forward',
        #    [self.KeyField('$MATCH_PRIORITY', self.to_bytes(0, 4)),
        #     self.KeyField('vrf', '\x00\x00'),
        #     self.KeyField('hdr.ipv4.dst_addr',
        #                   self.ipv4_to_bytes(dip),
        #                   self.ipv4_to_bytes('255.255.0.0'))],
        #    'SwitchIngress.hit',
        #    [self.DataField('port', '\x00\x03')])


        logger.info("Sending packet on port %d", ig_port)
        testutils.send_packet(self, ig_port, str(pkt))

        logger.info("Expecting packet on port %d", eg_ports[0]) # Change this --> eg_port[0]
        testutils.verify_packets(self, exp_pkt, [eg_ports[0]])

        #self.delete_table_entry(
        #    target,
        #    'SwitchIngress.forward',
        #    [self.KeyField('$MATCH_PRIORITY', self.to_bytes(0, 4)),
        #     self.KeyField('vrf', '\x00\x00'),
        #     self.KeyField('hdr.ipv4.dst_addr',
        #                   self.ipv4_to_bytes(dip),
        #                   self.ipv4_to_bytes('255.255.0.0'))])

        logger.info("Sending packet on port %d", ig_port)
        testutils.send_packet(self, ig_port, str(pkt))

        logger.info("Expecting packet on port %d", eg_ports[0])
        testutils.verify_packets(self, exp_pkt, [eg_ports[0]])

        self.delete_table_entry(
            target,
            'SwitchIngress.forward',
            [self.KeyField('$MATCH_PRIORITY', self.to_bytes(1, 4)),
             self.KeyField('vrf', '\x00\x00'),
             self.KeyField('hdr.ipv4.dst_addr',
                           self.ipv4_to_bytes(dip),
                           self.ipv4_to_bytes('255.255.0.0'))])

        logger.info("Sending packet on port %d", ig_port)
        testutils.send_packet(self, ig_port, str(pkt))

        logger.info("Packet is expected to get dropped.")
        testutils.verify_no_other_packets(self)
